package com.example.demo.controller;

import com.example.demo.pojo.Course;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.Rule;
import com.example.demo.service.CourseService;
import com.example.demo.service.RuleService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Resource
    private RuleService ruleService; // 注入Service

    @PostMapping("/add")
    public Result<Rule> addRule(@RequestBody Rule rule) {
        ruleService.saveRule(rule);
        return Result.success();
    }

    @DeleteMapping("/deleteCourse")
    public Result<String> deleteCourse(Integer courseId) {
        courseService.deleteCourse(courseId);
        return Result.success();
    }

    @RequestMapping("/updateCourse")
    public Result<String> updateCourse(Course course) {
        courseService.updateCourse(course);
        return Result.success();
    }

    @RequestMapping("/selectById")
    public Result<Course> selectById(Integer courseId) {
        Course course = courseService.selectById(courseId);
        return Result.success(course);
    }

    @RequestMapping("/selectAll")
    public Result<List<Course>> getAllCourses() {
        List<Course> courses = courseService.selectAll();
        return Result.success(courses);
    }

    @RequestMapping("/selectByName")
    public Result<List<Course>> searchCoursesByNameKeyword(String keyword) {
        List<Course> courses = courseService.selectByNameKeyword(keyword);
        return Result.success(courses);
    }
}